package cultu.customer.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import cultu.core.message.Context;
import cultu.customer.dao.OfferDAO;
import cultu.customer.message.Offer;
import cultu.customer.message.OfferCriteria;
import cultu.customer.message.OfferList;
import cultu.customer.message.OfferScenario;
import cultu.customer.service.OfferManager;

/**
 * Offer service implementation.
 * @author Marc-Antoine
 *
 */
@Transactional
public class OfferManagerImpl implements OfferManager {

  /**
   * Offer Dao.
   */
  private OfferDAO offerDao;

  /**
   * Sets offer Dao.
   * @param dao the dao to set.
   */
  public void setOfferDao(final OfferDAO dao){
    this.offerDao = dao;
  }

  /**
   * {@inheritDoc}
   */
  @Transactional(readOnly = true)
  public Offer getOffer(final Context pContext, final OfferScenario pScenario, final Offer pKey) {

    Offer result = null;

    // Check object.
    if (validateOffer(pKey)) {
      result = offerDao.get(pKey, pScenario);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Offer createOffer (final Context pContext, final OfferScenario pScenario, final Offer pOffer) {

    Offer result = null;

    // Check.
    if (validateOffer(pOffer)) {
      result = offerDao.create(pOffer);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Offer deleteOffer(final Context pContext, final OfferScenario pScenario, final Offer pKey) {
    Offer result =null;

    // Check.
    if(validateOffer(pKey)) {
      result = offerDao.delete(pKey);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Offer updateOffer(final Context pContext, final OfferScenario pScenario, final Offer pOffer) {

    Offer result =null;

    // Check.
    if (validateOffer(pOffer)) {
      result = offerDao.update(pOffer);
    }
    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OfferList findOffers(final Context pContext, final OfferScenario pScenario, final OfferCriteria pCriteria) {

    OfferList result = new OfferList();

    if (pCriteria != null) {
      List<Offer> myList = offerDao.find(pCriteria, pScenario);
      result.getOffers().addAll(myList);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OfferList deleteOffers(final Context pContext, final OfferScenario pScenario, final OfferList pList) {

    OfferList result = new OfferList();

    if (pList != null) {
      for (Offer myUser : pList.getOffers()) {
        result.getOffers().add(deleteOffer(pContext, pScenario, myUser));
      }
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OfferList createOffers(final Context pContext, final OfferScenario pScenario, final OfferList pList) {

    OfferList result = new OfferList();

    if (pList != null) {
      for (Offer myUser : pList.getOffers()) {
        result.getOffers().add(createOffer(pContext, pScenario,  myUser));
      }
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OfferList updateOffers(final Context pContext, final OfferScenario pScenario, final OfferList pList) {

    OfferList result = new OfferList();

    if (pList != null) {
      for (Offer myUser : pList.getOffers()) {
        result.getOffers().add(updateOffer(pContext, pScenario,  myUser));
      }
    }

    return result;
  }

  /**
   * Check that given object has enough data to be used.
   *
   * @param offer th offer to check.
   * @return true, if successful
   */
  private boolean validateOffer (final Offer offer) {

    // pas de controle sur receipt
    return offer != null && offer.getStartDate() != null && offer.getEndDate() != null && 
        (offer.getProduct() != null || offer.getCustomer() != null);
  }
}
